using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Factories;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using XNA_2DFramwork;

namespace ArtificialIntelligence.Racing
{
    public class RacePlayer:Cars
    {
        public static float playerS = 0;
        public int Score;
        static public int Nitrous;
        SpriteFont Font;
        public static int MaxSpeed;
        //Sound Sound;
        public RacePlayer(Game game, Vector2 position, string filename, PhysicsSimulator PhysicsSimulator)
            : base(game)
        {
            Nitrous = 3;
         //   Sound = new Sound();
            this.Score = 0;
            MaxSpeed = 11;
            Type = ObjectType.Player;
            Position = position;

            Texture = base.Game.Content.Load<Texture2D>(filename);
            Width = Texture.Width;
            Height = Texture.Height;
            Font = game.Content.Load<SpriteFont>("SpriteFont1");
            Body = Body = BodyFactory.Instance.CreateRectangleBody(PhysicsSimulator, Width, Height, 1);
            Body.Position = position;
            Geom = GeomFactory.Instance.CreateRectangleGeom(PhysicsSimulator, Body, Width, Height);
            Body.IgnoreGravity = true;
            Geom.CollisionResponseEnabled = false;
            SpriteBatch = new SpriteBatch(Game.GraphicsDevice);
        }
        public override void Draw()
        {
            //  SpriteBatch.Draw(Texture, Body.Position, r, Color.White, Body.Rotation, Origin, 1f, SpriteEffects.None,0f);
            SpriteBatch.Begin();
            SpriteBatch.Draw(Texture,Body.Position,null,Color.White,Body.Rotation,Origin,1f,SpriteEffects.None,0f);
            DrawText();
            SpriteBatch.End();
        }
        public void Move(float x, float y)
        {
            Body.Position = Position;
            if (City.CurrentLenght < City.RacerLenght)
            {
                if (x < 0)
                {
                    if (Position.X >= 250)
                        Position.X += x;
                    else
                    {
                        Position.X -= x;
                        if (playerS > 0)
                            playerS -= 5f;
                    }
                }
                if (x > 0)
                {
                    if (this.Position.X <= 1000)
                        this.Position.X += x;
                    else
                    {
                        this.Position.X -= x;
                        if (RacePlayer.playerS > 0)
                            RacePlayer.playerS -= 5f;
                    }
                }
            }
        }
        public override void Update()
        {
            foreach (VisualObject obj in VisualObject._lVisualObject)
                if (VisualObject.IsCollide(ref this.Geom,ref obj.Geom)) 
                {
                    switch (obj.Type)
                    {
                        case ObjectType.Money:
                            {
                                obj.Move(-1000, 0);
                                this.Score += 1000;
                                break;
                            }
                        case ObjectType.Nitrous:
                            {
                                obj.Move(-1000, 0);
                                if (Nitrous < 10)
                                    Nitrous += 1;
                                break;
                            }
                        case ObjectType.Obstacles:
                            {
                                playerS = 0;
                                break;
                            }
                    }
                }
        }
        protected void DrawText()
        {
            int speed = (int) playerS * 10;
            SpriteBatch.DrawString(Font, "Score: " + Score.ToString(), new Vector2(10, 10), Color.Red);
            SpriteBatch.DrawString(Font, "Speed: " + speed.ToString(), new Vector2(10, 50), Color.Red);
            SpriteBatch.DrawString(Font, "Lenght: " + City.CurrentLenght.ToString(), new Vector2(10, 90), Color.Red);
            SpriteBatch.DrawString(Font, "Nitros:" + Nitrous.ToString(), new Vector2(10, 130), Color.Red);
        }
    }
}